//
//  WBNavBar.m
//  WeatherBar
//
//  Created by yby on 2024/6/7.
//

#import "WBNavBar.h"

@interface WBNavBar () <UITextFieldDelegate>
@property (nonatomic ,assign) WBNavBarType var_type;
/// 搜索框
@property (nonatomic ,strong) UIView *var_searchView;
/// 底部的线
@property (nonatomic ,strong) UIView *var_bottomLineView;

@property (nonatomic ,strong) UIButton *var_backButton;
/// 大标题
@property (nonatomic ,strong) UILabel *var_titleLabel;
/// 子标题
@property (nonatomic ,strong) UILabel *var_subTitleLabel;
/// 俱乐部头像
@property (nonatomic ,strong) UIImageView *var_iconView;
/// 搜索图标
@property (nonatomic ,strong) UIImageView *var_searchIconView;
@end

@implementation WBNavBar

static CGFloat var_searchBarH = 32.0;
static CGFloat var_iconWH = 33.0;
- (instancetype)initWithType:(WBNavBarType)type
{
    if (self = [super init]) {
        _var_type = type;
        var_searchBarH = Ratio(33.0);
        var_iconWH = Ratio(33.0);
        
        self.frame = CGRectMake(0, 0, kScreenWidth, [UIDevice wb_fullNavHeight]);
        self.backgroundColor = [UIColor color8744DF];
        self.userInteractionEnabled = YES;
        _var_didClickSearchSubject = [RACSubject subject];
        _var_didClickBackSubject = [RACSubject subject];
        _var_didClickRithtSubject = [RACSubject subject];
        _var_didClickSwichSubject = [RACSubject subject];
        [self wb_setSubViews];
        [self wb_setConstraints];
        if (type==WBNavBarTypeNormal){
            [self wb_setNavbarStyleNormal];
        } else if (type==WBNavBarTypeRoot){
            [self wb_setNavbarStyleRoot];
        }
        else if (type==WBNavBarTypeOnlySearch) {
            [self wb_setupOnlySearch];
        } else if (type==WBNavBarTypeSearchBack){
            [self wb_setupSearchBack];
        } else if (type==WBNavBarTypeSearchAll){
            [self wb_setupSeachAll];
        } else if (type==WBNavBarTypeOnlyLeftTitle){
            [self wb_setOnlyLeftTitle];
        } else if (type==WBNavBarTypeIconSearch){
            [self wb_setIconSearch];
        } else if (type==WBNavBarTypeTileSubTileBack){
            [self wb_setTileSubTileBack];
        } else if (type==WBNavBarTypeLRTileSubTileBack){
            [self wb_setLRTileSubTileBack];
        }
        
    }
    return self;
}
- (void)wb_setSubViews
{
    [self addSubview:self.var_backBtn];
    [self addSubview:self.var_searchView];
    [self addSubview:self.var_rightBtn];
    [self addSubview:self.var_rightSecondBtn];
    [self addSubview:self.var_bottomLineView];
    [self addSubview:self.var_titleLabel];
    [self addSubview:self.var_subTitleLabel];
    [self addSubview:self.var_iconView];
}
- (void)wb_setConstraints
{
//    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(8);
//        make.bottom.mas_equalTo(-5);
//        make.size.mas_equalTo(CGSizeMake(40, 40));
//    }];
    [self.var_rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo (35);
        make.width.mas_greaterThanOrEqualTo(35).priorityLow();
//        make.size.mas_equalTo(CGSizeMake(35, 35));
        make.centerY.equalTo(self.var_backBtn).offset(-2);
    }];
    [self.var_rightSecondBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(35, 35));
        make.right.mas_equalTo(self.var_rightBtn.mas_left).offset(-12);
        make.centerY.mas_equalTo(self.var_rightBtn);
    }];
    [self.var_searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.var_backBtn.mas_right).offset(5);
        make.right.equalTo(self.var_rightBtn.mas_left).offset(-5);
        make.centerY.equalTo(self.var_backBtn);
        make.height.mas_equalTo(var_searchBarH);
    }];
    [self.var_bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-kLineHeight);
        make.height.mas_equalTo(kLineHeight);
    }];
    [self.var_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.var_backBtn.mas_right).offset(5);
        make.centerY.equalTo(self.var_backBtn);
    }];
    [self.var_subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.var_titleLabel);
        make.centerY.equalTo(self.var_titleLabel);
    }];
    [self.var_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.bottom.mas_equalTo(-8);
        make.width.height.mas_equalTo(var_iconWH);
    }];
}
#pragma mark - publicMethods
- (void)wb_setBackIcon:(UIImage *)icon
{
    self.var_backBtn.hidden = NO;
    [self.var_backButton setImage:icon forState:UIControlStateNormal];
    [self wb_adjustCenterTitle];
}
- (void)wb_setupTransparent
{
    self.var_bottomLineView.hidden = YES;
    self.backgroundColor = [UIColor clearColor];
}
- (void)wb_setupAlpha:(CGFloat)alpha
{
    if (alpha==1) {

        if (self.var_leftSearchBtn.superview) {
            UIColor *var_grayColor = RGBA(170, 170, 170, 1.0);
            self.var_leftSearchBtn.tintColor =var_grayColor;
            [self.var_leftSearchBtn setTitleColor:var_grayColor forState:(UIControlStateNormal)];
            UIView *var_lineView = [self.var_searchView viewWithTag:1001];
            var_lineView.backgroundColor = var_grayColor;
        }
    }else{
        
        if (self.var_leftSearchBtn.superview) {
            self.var_leftSearchBtn.tintColor = [UIColor colorFFF];
            [self.var_leftSearchBtn setTitleColor:[UIColor colorFFF] forState:(UIControlStateNormal)];
            UIView *var_lineView = [self.var_searchView viewWithTag:1001];
            var_lineView.backgroundColor = RGBA(255,255,255,0.2);
        }
    }
    self.backgroundColor = [UIColor colorWithHexString:@"#111111" alpha:alpha];
}
#pragma mark - privateMethods
- (void)wb_setupOnlySearch
{
    self.var_searchView.hidden = NO;
    [self.var_searchView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(var_searchBarH);
        make.centerY.equalTo(self.var_backBtn);
    }];
}
- (void)wb_setupSearchBack
{
    self.var_searchView.hidden = NO;
    self.var_backBtn.hidden = NO;
}
- (void)wb_setupSeachAll
{
    self.var_searchView.hidden = NO;
    self.var_backBtn.hidden = NO;
    [self.var_backButton setImage:[UIImage imageNamed:@"icon_navBack"] forState:UIControlStateNormal];

    self.var_textField.userInteractionEnabled = YES;

    self.var_rightBtn.hidden = NO;
    [_var_rightBtn setTitle:@"Search" forState:UIControlStateNormal];
    [_var_rightBtn setTitleColor:[UIColor color46464D] forState:UIControlStateNormal];
    
    self.var_rightSecondBtn.hidden = YES;
}
- (void)wb_setNavbarStyleNormal
{
    self.var_backBtn.hidden = NO;
    [self.var_backButton setImage:[UIImage imageNamed:@"icon_navBack"] forState:UIControlStateNormal];
    [self wb_adjustCenterTitle];
}
- (void)wb_setNavbarStyleRoot
{
    self.var_backBtn.hidden = YES;
    [self.var_backButton setImage:[UIImage imageNamed:@"icon_navBack"] forState:UIControlStateNormal];
    [self wb_adjustCenterTitle];
}


- (void)wb_setOnlyLeftTitle
{
    self.var_titleLabel.font = kBoldFontSize(16);
    [self.var_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(Ratio(16));
        make.centerY.equalTo(self.var_backBtn);
    }];
}
- (void)wb_setIconSearch
{
    self.var_iconView.hidden = NO;
    self.var_searchView.hidden = NO;
    [self.var_searchView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.var_iconView.mas_right).offset(15);
        make.centerY.equalTo(self.var_iconView);
        make.right.mas_equalTo(-63);
        make.height.mas_equalTo(Ratio(33));
    }];
}
- (void)wb_setTileSubTileBack
{
    self.var_backBtn.hidden = NO;
    self.var_titleLabel.hidden = NO;
    self.var_subTitleLabel.hidden = NO;
    [self.var_backButton setImage:[UIImage imageNamed:@"icon_navBack"] forState:UIControlStateNormal];
    [self.var_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.var_backBtn.mas_centerY).offset(-10);
        make.left.equalTo(self.var_backBtn.mas_right).offset(10);
        make.right.equalTo(self.var_rightBtn.mas_left).offset(-10);
    }];
    [self.var_subTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.var_titleLabel);
        make.top.equalTo(self.var_titleLabel.mas_bottom).offset(5);
    }];
}
- (void)wb_setLRTileSubTileBack
{
    [self wb_setTileSubTileBack];
    self.var_rightBtn.hidden = NO;
}
- (void)setVar_placeHolder:(NSString *)placeHolder
{
    self.var_textField.text = placeHolder;
}
- (void)setVar_title:(NSString *)title
{
    self.var_titleLabel.text = title;
}
- (void)setVar_subTitle:(NSString *)subTitle
{
    self.var_subTitleLabel.text = subTitle;
}
- (void)wb_setTitleVisible
{
    self.var_titleLabel.hidden = NO;
}
- (void)wb_setTitleHidden
{
    self.var_titleLabel.hidden = YES;
}
- (void)wb_setRightImg:(UIImage *)image {
    self.var_rightBtn.hidden = NO;
    [self.var_rightBtn setImage:image forState:UIControlStateNormal];
}

- (void)wb_setRightSecondImg:(UIImage *)image {
    self.var_rightSecondBtn.hidden = NO;
    [self.var_rightSecondBtn setImage:image forState:UIControlStateNormal];
}

- (void)wb_adjustCenterTitle {
    self.var_titleLabel.hidden = NO;
    [self.var_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        CGFloat var_offset = kIsBangsScreen ? 20 : 10;
        make.centerY.equalTo(self).offset(var_offset);
    }];
}


// 显示搜索框左按钮更新名称
- (void)wb_showAndUpdateLeftSearchBtWithName:(NSString *)name
{
    if (!self.var_leftSearchBtn.superview) {
        // 加载重新布局
        [self.var_searchView addSubview:self.var_leftSearchBtn];
        [self.var_leftSearchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(3);
            make.centerY.equalTo(self->_var_searchView);
            make.height.equalTo(self->_var_searchView);
            make.width.mas_equalTo(55);
        }];
        // 竖线
        UIView *var_lineView = [UIView new];
        var_lineView.backgroundColor = RGBA(255, 255, 255, 0.2);
        [self.var_searchIconView addSubview:var_lineView];
        var_lineView.tag = 1001;
        [var_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.var_leftSearchBtn.mas_right).offset(2);
            make.height.mas_equalTo(18);
            make.centerY.equalTo(self->_var_searchView);
            make.width.mas_equalTo(0.5);
            
        }];
        
        [self.var_searchIconView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.var_leftSearchBtn.mas_right).offset(9);
            make.centerY.equalTo(self->_var_searchView);
            make.size.mas_equalTo(CGSizeMake(13, 13));
        }];
    }
    [self.var_leftSearchBtn setTitle:name forState:(UIControlStateNormal)];
    
    [self.var_leftSearchBtn wb_setImagePositionStyle:(OrgImagePositionStyleRight) spacing:3];
    
}

- (void)wb_setIocnImage:(NSString *)imageNmae
{
    if ([imageNmae hasPrefix:@"http"]) {
//        [self.var_iconView sd_setImageWithURL:[NSURL URLWithString:imageNmae] placeholderImage:[UIImage imageNamed:@"home_defaultcoc"]];
    }else{
        if (imageNmae==nil) {
            imageNmae = @"home_defaultcoc";
        }
        self.var_iconView.image = [UIImage imageNamed:imageNmae];
    }
}
- (void)wb_setSearchBlackIocn
{
    self.var_searchIconView.image = [UIImage imageNamed:@"navsearch_icon_gray"];
}
#pragma mark - lazyLoading
- (UIView *)var_bottomLineView
{
    if (_var_bottomLineView==nil) {
        _var_bottomLineView = [[UIView alloc] init];
        _var_bottomLineView.backgroundColor = [UIColor colorEEE];
        _var_bottomLineView.hidden = YES;
    }
    return _var_bottomLineView;
}
- (UIView *)var_backBtn
{
    if (_var_backBtn == nil) {
        _var_backBtn = [[UIView alloc] init];
        _var_backBtn.frame = CGRectMake(8, self.bounds.size.height-45, 40, 40);
        _var_backBtn.userInteractionEnabled = YES;
        UIButton *var_btn = [[UIButton alloc] init];
        var_btn.userInteractionEnabled = NO;
        [var_btn setImage:[UIImage imageNamed:@"top_back_white"] forState:UIControlStateNormal];
        [_var_backBtn addSubview:var_btn];
        [var_btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self->_var_backBtn);
            make.size.mas_equalTo(CGSizeMake(36, 36));
        }];
        _var_backButton = var_btn;
        UITapGestureRecognizer *var_tap = [[UITapGestureRecognizer alloc] init];
        @weakify(self);
        [var_tap.rac_gestureSignal subscribeNext:^(NSNumber *x) {
            @strongify(self);
            [self.var_didClickBackSubject sendNext:nil];
        }];
        [_var_backBtn addGestureRecognizer:var_tap];

        _var_backBtn.hidden = YES;
    }
    return _var_backBtn;
}
- (UIButton *)var_leftSearchBtn
{
    if (!_var_leftSearchBtn) {
        _var_leftSearchBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _var_leftSearchBtn.titleLabel.font = kFontSize(12);
        [_var_leftSearchBtn setTitleColor:[UIColor colorFFF] forState:(UIControlStateNormal)];
        _var_leftSearchBtn.tintColor = [UIColor colorFFF];
        [_var_leftSearchBtn setImage:[[UIImage imageNamed:@"downward_arrow"] imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)] forState:(UIControlStateNormal)];
        @weakify(self);
        [[_var_leftSearchBtn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(id x) {
            @strongify(self);
            [self.var_didClickSwichSubject sendNext:nil];
        }];
        _var_leftSearchBtn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    }
    return _var_leftSearchBtn;
}

- (UIButton *)var_rightBtn
{
    if (_var_rightBtn == nil) {
        _var_rightBtn = [[UIButton alloc] init];
        [_var_rightBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
        _var_rightBtn.hidden = YES;
        @weakify(self);
        [[_var_rightBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            [self.var_didClickRithtSubject sendNext:nil];
        }];
    }
    return _var_rightBtn;
}
- (UIButton *)var_rightSecondBtn
{
    if (_var_rightSecondBtn == nil) {
        _var_rightSecondBtn = [[UIButton alloc] init];
        [_var_rightSecondBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
        _var_rightSecondBtn.hidden = YES;
        @weakify(self);
        [[_var_rightSecondBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            [self.var_didClickRithtSubject sendNext:nil];
        }];
    }
    return _var_rightSecondBtn;
}

- (UIView *)var_searchView
{
    if (_var_searchView==nil) {
        _var_searchView = [[UIView alloc] init];
        _var_searchView.layer.cornerRadius = var_searchBarH*0.5;
        _var_searchView.layer.masksToBounds = YES;
        _var_searchView.layer.borderWidth = 0.5;
        _var_searchView.layer.borderColor = [UIColor colorEBEBEB].CGColor;
        _var_searchView.backgroundColor = [UIColor colorFAFAFA];
        UIImageView *var_iconView = [[UIImageView alloc] init];
        var_iconView.image = [UIImage imageNamed:@"nav_search"];
        self.var_searchIconView = var_iconView;
        [_var_searchView addSubview:var_iconView];

        [_var_searchView addSubview:self.var_textField];
        [var_iconView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(8);
            make.centerY.equalTo(self->_var_searchView);
            make.size.mas_equalTo(CGSizeMake(24, 24));
        }];
        [_var_textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(var_iconView.mas_right).offset(6);
            make.centerY.equalTo(self->_var_searchView);
            make.right.mas_equalTo(-12);
        }];
        _var_searchView.userInteractionEnabled = YES;
        UITapGestureRecognizer *var_tap = [[UITapGestureRecognizer alloc] init];
        @weakify(self);
        [var_tap.rac_gestureSignal subscribeNext:^(NSNumber *x) {
            @strongify(self);
            [self.var_didClickSearchSubject sendNext:nil];
        }];
        [_var_searchView addGestureRecognizer:var_tap];
        _var_searchView.hidden = YES;
    }
    return _var_searchView;
}
- (UITextField *)var_textField {
    if (!_var_textField) {
        _var_textField = [[UITextField alloc] init];
        _var_textField.font = [UIFont systemFontOfSize:14];
        _var_textField.tintColor = [UIColor colorB0B0BF];
        _var_textField.textColor = [UIColor color46464D];
        _var_textField.placeholder = @"Enter word search";
//        _textField.keyboardType = UIKeyboardTypeDefault;
        _var_textField.returnKeyType = UIReturnKeySearch;
        _var_textField.delegate = self;
        _var_textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _var_textField.enablesReturnKeyAutomatically = YES;
        _var_textField.keyboardAppearance = UIKeyboardAppearanceLight;

        [_var_textField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];

    }
    return _var_textField;

}

- (UILabel *)var_titleLabel
{
    if (_var_titleLabel == nil) {
        _var_titleLabel = [[UILabel alloc] init];
        _var_titleLabel.textColor = [UIColor colorFFF];
        _var_titleLabel.font = [UIFont systemFontOfSize:Ratio(18) weight:UIFontWeightMedium];
        _var_titleLabel.textAlignment = NSTextAlignmentCenter;
        _var_titleLabel.hidden = YES;
    }
    return _var_titleLabel;
}
- (UILabel *)var_subTitleLabel
{
    if (_var_subTitleLabel == nil) {
        _var_subTitleLabel = [[UILabel alloc] init];
        _var_subTitleLabel.textColor = [UIColor colorFFF];
        _var_subTitleLabel.font = kFontSize(11);
        _var_subTitleLabel.textAlignment = NSTextAlignmentCenter;
        _var_subTitleLabel.hidden = YES;
    }
    return _var_subTitleLabel;
}
- (UIImageView *)var_iconView
{
    if (_var_iconView == nil) {
        _var_iconView = [[UIImageView alloc] init];
        _var_iconView.layer.masksToBounds = YES;
        _var_iconView.layer.cornerRadius = var_iconWH*0.5;
        _var_iconView.image = [UIImage imageNamed:@"home_defaultcoc"];
        _var_iconView.hidden = YES;
    }
    return _var_iconView;
}

#pragma mark - textFiledDelegate

-(void)doResignFistResponder
{
    [self.var_textField resignFirstResponder];
    self.var_textField.text = @"";
//    if (self.doCancelBlock) {
//        self.doCancelBlock();
//    }
}
- (void)textChange:(UITextField *)textField {
    NSString *var_lang = [[UITextInputMode activeInputModes].firstObject primaryLanguage];
    if([var_lang isEqualToString:@"zh-Hans"]){ //简体中文输入，包括简体拼音，简体五笔，简体手写
        UITextRange *var_selectedRange = [self.var_textField markedTextRange];
        UITextPosition *var_position = [self.var_textField positionFromPosition:var_selectedRange.start offset:0];
        if (var_position){//高亮
            return;
        }
    }
    
//    if (self.searchChangeTextBlock) {
//        self.searchChangeTextBlock(self.textField.text);
//    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    return YES;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}
//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [super touchesBegan:touches withEvent:event];
//    [_textField becomeFirstResponder];
//}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    
    if (textField.text.length == 0) {
        return NO;
    }
    if (self.var_searchTextBlock) {
        self.var_searchTextBlock(textField.text);
    }
    return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
//    if (self.searchDidBeginEditingBlock) {
//        self.searchDidBeginEditingBlock();
//    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
//    if (self.searchDidEndEditingBlock) {
//        self.searchDidEndEditingBlock();
//    }
    if (textField.text.length) {
        return;
    }
}

@end
